/**
 * Math.java
 * @author: liuex
 * 2008-3-12 ����02:42:59
 *
 * note: 
 */
package org.lex.math;

/**
 * 
 */
public class Math {
	public static long lcm(long a, long b) {
		boolean negative = false;
		if (a < 0) {
			a = -a;
			negative = !negative;
		}
		if (b < 0) {
			b = -b;
			negative = !negative;
		}
		long gcd = gcd(a, b);
		long lcm = a * (b / gcd);
		return negative ? -lcm : lcm;
	}

	public static long gcd(long a, long b) {
		// we make sure that a <= b
		if (a < b) {
			long temp = a;
			a = b;
			b = temp;
		}
		return Math.gcd0(a, b);
	}

	private static long gcd0(long greater, long lesser) {
		long remainder = greater % lesser;
		if (0L == remainder)
			return lesser;
		return gcd0(lesser, remainder);
	}
}
